/**
 * Created by smp on 2017/4/1.
 */
var modelPath=ROOT_PATH+'/observation/observationCard/';
/**
 * 关闭layer
 */
function closeLayer() {
    layer.close(index);
}
/**
 * 使用layer 打开一个页面
 * @param title
 * @param content
 */
function openLayer(title,content) {

    index=layer.open({
        type: 2,
        title: title,
        shadeClose: true,
        area: ['1000px', '700px'],
        content: modelPath+content
    });
}
function result(data) {
    if(data==1){
        layer.msg("保存成功");
        closeLayer();
    }else{
        layer.alert("保存失败");
    }
}
function editCardFlash(cid) {
    var title='编辑观察卡';
    var content='editCard.shtml?id='+cid;
    openLayer(title,content);
}
var tree=null;
var getSelectedUser = function (data) {
    if(tree=="analysisTree"){
        $("#analysis").val(data[0].id);
        $("#analysisTree").textbox("setValue",data[0].realName);
    }else if(tree=="obsRep"){
        $("#obsReporter").val(data[0].id);
        $("#obsRep").textbox("setValue",data[0].realName);
    }else if(tree=="handlePeopleName"){
        $("#handlePeople").val(data[0].id);
        $("#handlePeopleName").textbox("setValue",data[0].realName);
    }else{
        $("#addUserId").val(data[0].id);
        $("#addUserIdName").textbox("setValue",data[0].realName);
    }
};
var getSelectedContractor=function (data) {
    data = data[0];
    $('#contractorName').textbox('setValue', data.name);
    $('#contractor').val(data.id);
};
function openDetailPage(id) {
    var title='观察卡详情';
    var content='detailCard.shtml?id='+id;
    openLayer(title,content);
}
/***
 * 根据业务id查询流程信息
 * @param row
 */
function flowMonitorByBusinessId(businessId) {
    var index = layer.open({
        type: 2,
        title: '业务流程监控',
        area: ['80%', '80%'],
        content: ROOT_PATH + '/workflow/flowManagement/flowMonitorByBusinessId.shtml?businessId='+businessId
    });
}
$(function () {
    var DEPTIdIFNULL=null;
    //初始化组织结构树
    $('#department_tree').tree({
        url: ROOT_PATH + '/permission/department/getDepartmentTree.shtml?parentId=0',
        lines:true,
        onSelect: function (node) {
            $('#tb1').form('reset');
            $('#tb2').form('reset');
            $(".unit").css("display","block");
            $(".contractor").css("display","none");
            //重新按部门加载datagrid
            $('#dg1').datagrid('load', {
                deptId: node.id,
                sort: 'addTime',
                order: 'desc'
            });
        },
        onLoadSuccess: function (node, data) {
            var deptId = data[0].id;
            DEPTIdIFNULL=deptId;
            //初始化加载datagrid
            $('#dg1').datagrid({
                method: 'post',
                url: ROOT_PATH + '/observation/observationCard/list.shtml',
                queryParams: {
                    deptId: deptId,
                    sort: 'addTime',
                    order: 'desc'
                },
                toolbar:'#toolbar',
                //idField: 'id',
                fitColumns: true,
                pagination: true,
                rownumbers: true,
                sortOrder: true,
                striped: true,
                singleSelect:true,
                fit: true,
                columns: [[
                    {field: 'id', width: 80, checkbox: true},
                    {field: 'obsName', title: '被观察作业', width: 100,formatter:function(value,row,index){
                        return "<a href='javascript:void(0);' style='color: deepskyblue;' onclick='openDetailPage(\""+row.id+"\")'>"+value+"</a>"
                    },  sortable: true},
                    {field: 'obsDescribe', title: '情况描述', width: 80,  sortable: true},
                    {field: 'obsFoundDate', title: '观察发现日期', width: 100, sortable: true},
                    {field: 'obsDeptName', title: '被观察单位/承包商', width: 100, sortable: false},
                    {field: 'obsReporterName', title: '报告人', width: 100, sortable: false},
                    {field: 'obsType', title: '观察类型', width: 100, sortable: true ,
                        formatter: function (value, row, index) {
                        if (row.obsType==0) {
                            return '不安全行为';
                        } else if(row.obsType==1){
                            return '不安全状态';
                        }else{
                            return '推荐的安全行为';
                        }
                    }},
                    {field: 'obsStatus', title: '状态', width: 100, sortable: true,
                        formatter:function(value,row,index){
                            if(value=="已登记"){
                                return "<span style='color: #FF0000;'>"+value+"</span>";
                            }else if(value=="等待分析"){
                                return "<a href='javascript:void(0);' onclick=\"flowMonitorByBusinessId('"+row.id+"')\" style='color: #0abffc;'>"+value+"</a>";
                            }else if(value=="等待处理"){
                                return "<a href='javascript:void(0);' onclick=\"flowMonitorByBusinessId('"+row.id+"')\" style='color: #0abffc;'>"+value+"</a>";
                            }else{
                                return "<a href='javascript:void(0);' onclick=\"flowMonitorByBusinessId('"+row.id+"')\" style='color: #FF8C00'>"+value+"</a>"
                            }
                        }
                    },
                    {field: 'obsReporterDeptName', title: '观察单位/部门', width: 100, sortable: false},
                    {field: 'obsAddDeptName', title: '登记部门', width: 100, sortable: false},
                    {field: 'addTime', title: '登记时间', width: 100, sortable: true},
                    {field: 'obsAddName', title: '登记人', width: 100, sortable: false}
                ]]
            });
        }
    });
    $('#btn_clear').on("click", function () {
        $('#tb1').form('reset');
        $('#tb2').form('reset');
        $(".unit").css("display","block");
        $(".contractor").css("display","none");
    });
    $('#btn_search').on("click", function () {
        var getSelected = $("#department_tree").tree('getSelected');
        var deptId=DEPTIdIFNULL;
        if(getSelected){
            deptId=getSelected.id
        }
        var obsName = $('#obsName').textbox("getValue");
        var obsDescribe = $('#obsDescribe').textbox("getValue");
        var obsType = $('#obsType').combobox("getValue");
        var isConstrctor = $('#isConstrctor').combobox("getValue");
        var obsDeptId = $('#obsDeptId').combotree("getValue");
        if(isConstrctor==1){
            obsDeptId = $('#contractor').val();
        }
        var obsReporter = $('#obsReporter').val();
        var analysisUser = $('#analysis').val();
        var handleUser = $('#handlePeople').val();
        var obsPlace = $('#obsPlace').textbox("getValue");
        var beginObsTime = $('#beginObsTime').datebox("getValue");
        var endObsTime = $('#endObsTime').datebox("getValue");
        var obsStatus = $('#obsStatus').textbox("getValue");
        var addDeptId = $('#addDeptId').combotree("getValue");
        var addUserId = $('#addUserId').val();
        var beginLoginTime = $('#beginLoginTime').datebox("getValue");
        var endLoginTime = $('#endLoginTime').datebox("getValue");
        $('#dg1').datagrid('load', {deptId:deptId,obsName:obsName,obsDescribe:obsDescribe,
            obsType:obsType,isConstrctor:isConstrctor,obsDeptId:obsDeptId,obsReporter:obsReporter,
            analysisUser:analysisUser,handleUser:handleUser,obsPlace:obsPlace,beginObsTime:beginObsTime,
            endObsTime:endObsTime,obsStatus:obsStatus,addDeptId:addDeptId,addUserId:addUserId,
            beginLoginTime:beginLoginTime,endLoginTime:endLoginTime, sort: 'addTime',
            order: 'desc'});
    });
    $("#obs_card_edit").on("click",function(){
        var checkedRows=$('#dg1').datagrid('getChecked');

        if(checkedRows.length == 0){
            layer.alert("选择需要编辑的行！");
            return
        }else if (checkedRows.length > 1){
            layer.alert("只能选中一行数据！");
            return
        }
        var id=checkedRows[0].id;
        var title='编辑观察卡';
        var content='editCard.shtml?id='+id;
        openLayer(title,content);
    });
    //添加按钮事件
    $('#obs_card_add').on('click',function () {

        var title='添加观察卡';
        var content='add.shtml';
        openLayer(title,content);
    });
    //删除按钮事件
    $('#obs_card_delete').on('click',function () {

        var rows = $('#dg1').datagrid('getSelections');

        if(rows.length != 1){
            layer.alert("请选择需要删除的行！");
            return
        }
        var id=rows[0].id;
        layer.confirm('是否确定删除当前选中数据？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.ajax({
                type:'post',
                url : modelPath+'delete.shtml',
                data :{id:id},
                dataType:"json",
                success: function (result) {
                    if (result.success) {
                        $('#dg1').datagrid('clearSelections').datagrid('load');
                        layer.msg('删除成功', {
                            icon: 1,
                            time: 1000
                        });
                    }
                    else {
                        layer.msg('删除失败', {
                            icon: 2,
                            time: 1000
                        });
                    }
                }
            });
        });

    });
    $('#btn_advanced_search').on("click", function () {
        $('#tb2').toggle();
        $('#dg1').datagrid('resize');
    });
    //初始化下拉树
    $('.parentDepartmentId').combotree({
        url: ROOT_PATH+'/permission/department/getDepartmentTree.shtml?parentId=0'
    });
    $('#analysisTree').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                tree="analysisTree";
                WindowManager.openLayer({
                    type: 2,
                    title: '添加分析人',
                    area: ['90%', '70%'],
                    content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
                    callBack:getSelectedUser
                });
            }
        }],
        editable: false
    });
    $('#obsRep').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                tree="obsRep";
                WindowManager.openLayer({
                    type: 2,
                    title: '添加报告人',
                    area: ['90%', '70%'],
                    content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
                    callBack:getSelectedUser
                });
            }
        }],
        editable: false
    });
    $('#handlePeopleName').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                tree="handlePeopleName";
                WindowManager.openLayer({
                    type: 2,
                    title: '添加处理人',
                    area: ['90%', '70%'],
                    content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
                    callBack:getSelectedUser
                });
            }
        }],
        editable: false
    });
    $('#addUserIdName').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                tree="addUserIdName";
                WindowManager.openLayer({
                    type: 2,
                    title: '添加登记人',
                    area: ['90%', '70%'],
                    content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
                    callBack:getSelectedUser
                });
            }
        }],
        editable: false
    });
    $('#contractorName').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                WindowManager.openLayer({
                    type: 2,
                    title: '选择承包商',
                    area: ['90%', '70%'],
                    content: ROOT_PATH + '/contractor/contractorInfo/simpleList.shtml',
                    callBack:getSelectedContractor
                });
            }
        }],
        editable: false
    });
    $("#isConstrctor").combobox({
        onChange:function (newValue,oldValue) {
            if(newValue==1){
                $(".unit").css("display","none");
                $(".contractor").css("display","inline");
            }else{
                $(".unit").css("display","inline");
                $(".contractor").css("display","none");
            }
        }
    });
});